package com.crk.yiyuanguahao.service.impl;

import com.crk.yiyuanguahao.dao.DoctorCommentDao;
import com.crk.yiyuanguahao.dao.DoctorDao;
import com.crk.yiyuanguahao.dao.DoctorFeedbackDao;
import com.crk.yiyuanguahao.entity.Doctor;
import com.crk.yiyuanguahao.entity.DoctorComment;
import com.crk.yiyuanguahao.entity.DoctorFeedback;
import com.crk.yiyuanguahao.entity.DoctorFeedbackDto;
import com.crk.yiyuanguahao.service.DoctorFeedbackService;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/3/26
 */
@Service
public class DoctorFeedbackServiceImpl implements DoctorFeedbackService {
    @Autowired
    DoctorFeedbackDao doctorFeedbackDao;
    @Autowired
    DoctorCommentDao doctorCommentDao;
    @Autowired
    DoctorDao doctorDao;
    /**
     * 保存医生反馈
     *
     * @param doctorFeedback 评价
     * @return
     */
    @Override
    public ResponseResult saveDoctorFeedback(DoctorFeedback doctorFeedback) {
        if (null != doctorFeedback){
            int result = 0;
            if (doctorFeedback.getId() == null){
                doctorFeedback.setCreateTime(new Date());
                doctorFeedback.setStatus(1);
                result = doctorFeedbackDao.insert(doctorFeedback);
            }else{
                result = doctorFeedbackDao.updateByPrimaryKey(doctorFeedback);
            }

            if (result > 0){
                return ResponseResult.success(doctorFeedback);
            }else{
                return ResponseResult.fail();
            }
        }else {
            return ResponseResult.fail();
        }
    }

    /**
     * 删除医生反馈
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteDoctorFeedback(Integer id) {
        int i = doctorFeedbackDao.deleteByPrimaryKey(id);
        if (i>0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 查询医生反馈
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getDoctorFeedback(Integer id) {
        DoctorFeedback doctorFeedback = doctorFeedbackDao.selectByPrimaryKey(id);
        DoctorFeedbackDto doctorFeedbackDto = new DoctorFeedbackDto();
        Integer commentId = doctorFeedback.getCommentId();
        DoctorComment doctorComment = doctorCommentDao.selectByPrimaryKey(commentId);
        doctorFeedbackDto.setCommentContent(doctorComment.getContent());
        doctorFeedbackDto.setId(doctorFeedback.getId());
        doctorFeedbackDto.setDoctorCode(doctorFeedback.getDoctorCode());
        doctorFeedbackDto.setReason(doctorFeedback.getReason());
        doctorFeedbackDto.setStatus(doctorFeedback.getStatus());
        doctorFeedbackDto.setCreateTime(doctorFeedback.getCreateTime());
        doctorFeedbackDto.setCommentId(doctorFeedback.getCommentId());
        Doctor doctor = doctorDao.selectDoctorByCode(doctorFeedback.getDoctorCode());
        doctorFeedbackDto.setDoctorName(doctor.getDoctorName());
        return ResponseResult.success(doctorFeedbackDto);
    }

    /**
     * 查询医生反馈列表
     *
     * @param status 医生编号
     * @param pageInfo   分页
     * @return
     */
    @Override
    public ResponseResult queryDoctorFeedbackPage(int status, Page pageInfo) {
        Integer count = doctorFeedbackDao.countByCondition(status);
        pageInfo.setAllRow(count);
        List<DoctorFeedback> doctorList = doctorFeedbackDao.selectByCondition(status, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorFeedbackDto> doctorFeedbackDtos = new ArrayList<>();
        for (DoctorFeedback doctorFeedback : doctorList) {
            DoctorFeedbackDto doctorFeedbackDto = new DoctorFeedbackDto();
            Integer commentId = doctorFeedback.getCommentId();
            DoctorComment doctorComment = doctorCommentDao.selectByPrimaryKey(commentId);
            if (null != doctorComment){
                doctorFeedbackDto.setCommentContent(doctorComment.getContent());
            }
            doctorFeedbackDto.setId(doctorFeedback.getId());
            doctorFeedbackDto.setDoctorCode(doctorFeedback.getDoctorCode());
            doctorFeedbackDto.setReason(doctorFeedback.getReason());
            doctorFeedbackDto.setStatus(doctorFeedback.getStatus());
            doctorFeedbackDto.setCreateTime(doctorFeedback.getCreateTime());
            doctorFeedbackDto.setCommentId(doctorFeedback.getCommentId());
            Doctor doctor = doctorDao.selectDoctorByCode(doctorFeedback.getDoctorCode());
            if (null != doctor){
                doctorFeedbackDto.setDoctorName(doctor.getDoctorName());
            }
            doctorFeedbackDtos.add(doctorFeedbackDto);
        }
        return ResponseResult.success(doctorFeedbackDtos);
    }

    /**
     * 审批
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseResult approval(int id, int status) {
        DoctorFeedback doctorFeedback = doctorFeedbackDao.selectByPrimaryKey(id);
        if (status == 2){
            Integer commentId = doctorFeedback.getCommentId();
            doctorCommentDao.deleteByPrimaryKey(commentId);
        }
        doctorFeedbackDao.updateStatus(id,status);
        return ResponseResult.success();
    }
}
